package microframe.abstract
{
	import com.ux.lg.ACEF_Tracking;
	import com.ux.manager.StageManager;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	import microframe.interfaces.IContents;
	import microframe.type.Position;
	
	/**
	 * @author winnie
	 */	
	public class AbsContents extends MovieClip implements IContents
	{
		// 서브일 경우 contentsType, position 을 반드시 등록할 것.
		private var _contentsType : String;
		private var _position : String;
		
		// resizing
		private var LTcontentsList : Array = [];
		private var LCcontentsList : Array = [];
		private var LBcontentsList : Array = [];
		private var RTcontentsList : Array = [];
		private var RCcontentsList : Array = [];
		private var RBcontentsList : Array = [];
		private var TCcontentsList : Array = [];
		private var BCcontentsList : Array = [];
		private var CNcontentsList : Array = [];
		private var dicPoint : Dictionary = new Dictionary();
		
		
		/**
		 * AbsContents
		 */		
		public function AbsContents()
		{
			super();
			
			this.addEventListener( Event.ADDED_TO_STAGE, onStage , false, 0, true );
			
			initLayOut();
		}
		
		
		/**
		 * 스테이지에 컨텐츠가 붙음.
		 */		
		protected function onStage( e:Event ):void
		{
			e.currentTarget.removeEventListener( e.type, arguments.callee, false );

			if( StageManager.stage == null ) StageManager.stage = this.stage;
			StageManager.stage.align = StageAlign.TOP_LEFT;
			StageManager.stage.scaleMode = StageScaleMode.NO_SCALE;
			if( this.parent == StageManager.stage ) init();
			
			this.startResizing();
			this.onResize();
			this.addEventListener( Event.REMOVED_FROM_STAGE, onRemoveStage );
		}
		
		
		/**
		 * 스테이지에서 컨텐츠가 제거 됨.
		 */
		protected function onRemoveStage( e:Event = null ):void
		{
			e.currentTarget.removeEventListener( e.type, arguments.callee, false );
			
			this.stopResizing();
		}
		
		
		/**
		 * IContents 구현 : 초기화
		 */		
		public function init():void
		{
			// LG 전용 에이스 카운터 콘텐츠
//			trace(" =========> ACEF_Tracking call : " + this._contentsType);
//			ACEF_Tracking.call( this._contentsType );
		}
		
		
		/**
		 * 최초 레이아웃 정의
		 */		
		public function initLayOut():void
		{
			
		}
		
		
		/**
		 * <br>IContents 구현 : 메모리 삭제</br>
		 * <br> Application 은 destory 가 필요 없음. </br>
		 */		
		public function destory():void
		{
			
		}
		
		/**
		 * IContents 구현 : 컨텐츠 타입 정의
		 * @param type : ContentsTypeManager 에 등록된 type으로 정의
		 */		
		public function set contentsType( type:String ):void
		{
			_contentsType = type;
		}
		
		/**
		 * IContents 구현 : 컨텐츠 타입 반환
		 * @return ContentsTypeManager 에 등록된 type으로 반환
		 */		
		public function get contentsType():String
		{
			return _contentsType;
		}
		
		/**
		 * IContents 구현 : 컨텐츠 포지션 형태 정의
		 * @param type : Position 정의
		 */		
		public function set position( pos:String ):void
		{
			_position = pos;
		}
		
		/**
		 * IContents 구현 : 컨텐츠 포지션 형태 반환
		 * @return Position 반환
		 */		
		public function get position():String
		{
			return _position;
		}
		
		
		
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// resizing
		//
		//-----------------------------------------------------------------------------------
		
		/**
		 * 1개씩 추가하기
		 * @param content 추가대상
		 * @param position LT, LB, RT, RB
		 * @param posX x 좌표 이격 거리 = 기본값은 0
		 * @param posY y 좌표 이격 거리 = 기본값은 0
		 */		
		protected function registerContent( content:DisplayObject, position:String, posX : Number = 0, posY : Number = 0 ):void
		{
			switch(position)
			{
				case Position.LEFT_TOP : checkAndPushAry(content, LTcontentsList ); break;
				case Position.LEFT_CENTER : checkAndPushAry(content, LCcontentsList ); break;
				case Position.LEFT_BOTTOM : checkAndPushAry(content, LBcontentsList ); break;
				case Position.RIGHT_TOP : checkAndPushAry(content, RTcontentsList ); break;
				case Position.RIGHT_CENTER : checkAndPushAry(content, RCcontentsList ); break;
				case Position.RIGHT_BOTTOM : checkAndPushAry(content, RBcontentsList ); break;
				case Position.TOP_CENTER : checkAndPushAry(content, TCcontentsList ); break;
				case Position.BTM_CENTER : checkAndPushAry(content, BCcontentsList ); break;
				case Position.CENTER : checkAndPushAry(content, CNcontentsList ); break;
			}
			
			dicPoint[ content ] = new Point( posX, posY );
			
		}
		
		
		private function checkAndPushAry( content:DisplayObject, ary:Array ):void
		{
			var len:int = ary.length;
			for (var i:int = 0; i < len; i++) 
			{
				if( content == ary[ i ] ) return;
			}
			
			ary.push( content );
		}
		
		
		/**
		 * 1개만 제거하려 할 경우.
		 * @param content 제거대상
		 */		
		protected function removeContent( content:DisplayObject ):void
		{

			removeContentAtArray( content, LTcontentsList );
			removeContentAtArray( content, LCcontentsList );
			removeContentAtArray( content, LBcontentsList );
			removeContentAtArray( content, RTcontentsList );
			removeContentAtArray( content, RCcontentsList );
			removeContentAtArray( content, RBcontentsList );
			removeContentAtArray( content, TCcontentsList );
			removeContentAtArray( content, BCcontentsList );
			removeContentAtArray( content, CNcontentsList );
		}
		
		
		// 배열에서 해당 컨텐츠를 제거하고, 컨텐츠를 removeChild 시키며, 컨텐츠를 null로 초기화 한다. 
		private function removeContentAtArray( content:DisplayObject, arr:Array ):void
		{

			var len:int = arr.length;
			for (var i:int = 0; i < len; i++) 
			{
				if( arr[ i ] == content )
				{
					delete( arr.splice( arr.indexOf( content ) , 1 ) );
				}
			}
		}
		
		
		/**
		 * 전체 컨텐츠 제거 및 초기화 
		 */		
		protected function removeAll():void
		{

			removeList( LTcontentsList );
			removeList( LCcontentsList );
			removeList( LBcontentsList );
			removeList( RTcontentsList );
			removeList( RCcontentsList );
			removeList( RBcontentsList );
			removeList( TCcontentsList );
			removeList( BCcontentsList );
			removeList( CNcontentsList );
		}
		
		
		// 배열에서 해당 컨텐츠를 제거하고, 컨텐츠를 removeChild 시키며, 컨텐츠를 null로 초기화 한다. 
		private function removeList( arr:Array ):void
		{
			var len:int = arr.length;
			for (var i:int = 0; i < len; i++) 
			{
				delete( arr.shift() );
			}
		}
		
		
		/**
		 * 리사이징 시작 이벤트 등록.
		 */		
		public function startResizing():void
		{
			StageManager.stage.addEventListener( Event.RESIZE, onResize );
		}
		
		
		/**
		 * 리사이징 정지를 위한 이벤트 제거
		 */		
		public function stopResizing():void
		{
			StageManager.stage.removeEventListener( Event.RESIZE, onResize );
		}
		
		
		/**
		 * 실제 리사이징 부분.
		 */		
		protected function onResize( e:Event=null ):void
		{
			// left btm
			var len:int = LBcontentsList.length;
			for (var i:int = 0; i < len; i++) 
			{
				LBcontentsList[ i ].x = dicPoint[ LBcontentsList[ i ] ].x;
				LBcontentsList[ i ].y = StageManager.stage.stageHeight + dicPoint[ LBcontentsList[ i ] ].y;
			}
			
			
			// left center
			len = LCcontentsList.length;
			for ( i = 0; i < len; i++) 
			{
				LCcontentsList[ i ].x = dicPoint[ LCcontentsList[ i ] ].x;
				LCcontentsList[ i ].y = ( StageManager.stage.stageHeight - LCcontentsList[ i ].height )/2 + dicPoint[ LCcontentsList[ i ] ].y;
			}
			
			
			// right top
			len = RTcontentsList.length;
			for ( i = 0; i < len; i++) 
			{
				RTcontentsList[ i ].x = StageManager.stage.stageWidth + dicPoint[ RTcontentsList[ i ] ].x;
				RTcontentsList[ i ].y = dicPoint[ RTcontentsList[ i ] ].y;
			}
			
			
			// right center
			len = RCcontentsList.length;
			for ( i = 0; i < len; i++) 
			{
				RCcontentsList[ i ].x = StageManager.stage.stageWidth + dicPoint[ RCcontentsList[ i ] ].x;
				RCcontentsList[ i ].y = ( StageManager.stage.stageHeight - RCcontentsList[ i ].height )/2 + dicPoint[ RCcontentsList[ i ] ].y;
			}
			
			// right btm
			len = RBcontentsList.length;
			for ( i = 0; i < len; i++) 
			{
				RBcontentsList[ i ].x = StageManager.stage.stageWidth + dicPoint[ RBcontentsList[ i ] ].x;
				RBcontentsList[ i ].y = StageManager.stage.stageHeight + dicPoint[ RBcontentsList[ i ] ].y;
			}
			
			// top center
			len = TCcontentsList.length;
			for ( i = 0; i < len; i++) 
			{
				TCcontentsList[ i ].x = ( StageManager.stage.stageWidth - TCcontentsList[ i ].width )/2 + dicPoint[ TCcontentsList[ i ] ].x;
				TCcontentsList[ i ].y = dicPoint[ TCcontentsList[ i ] ].y;
			}
			
			// btm center
			len = BCcontentsList.length;
			for ( i = 0; i < len; i++) 
			{
				BCcontentsList[ i ].x = ( StageManager.stage.stageWidth - BCcontentsList[ i ].width )/2 + dicPoint[ BCcontentsList[ i ] ].x;
				BCcontentsList[ i ].y = StageManager.stage.stageHeight + dicPoint[ BCcontentsList[ i ] ].y;
			}
			
			// center
			len = CNcontentsList.length;
			for ( i = 0; i < len; i++) 
			{
				CNcontentsList[ i ].x = ( StageManager.stage.stageWidth - CNcontentsList[ i ].width )/2 + dicPoint[ CNcontentsList[ i ] ].x;
				CNcontentsList[ i ].y = ( StageManager.stage.stageHeight - CNcontentsList[ i ].height )/2 + dicPoint[ CNcontentsList[ i ] ].y;
			}
		}
	}
}






